﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//using MyDataService;
using System.Configuration;
using System.Data.SqlClient;
using QuanLyBanHang.DAL;
using System.IO;
using QuanLyBanHang.Report;

namespace QuanLyBanHang
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            cboKieuTK.SelectedIndex = 0;
            txtTimKiem.Visible = false;
            //dataservice.strConn = ConfigurationManager.ConnectionStrings["strConn"].ConnectionString;
            try
            {
                StreamReader sr = new StreamReader("Config.ini");
                if (!sr.EndOfStream)
                {
                    string[] data = sr.ReadLine().Split('\t');
                    string dateNow = DateTime.Today.ToString("dd/MM/yyyy");
                    TimeSpan timeNow = DateTime.Now.TimeOfDay;
                    TimeSpan timeFile = TimeSpan.Parse(data[1]);
                    if (data[2] == "False" && data[0] == dateNow && timeFile <= timeNow)
                    {
                        string kt = this.Backup(data[3]);
                        if (kt == null)
                        {
                            MessageBox.Show("Sao lưu dữ liệu thành công !", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            sr.Close();
                            StreamWriter sw = new StreamWriter("Config.ini", false);
                            sw.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}",dateNow,timeNow.ToString(@"hh\:mm\:ss"), "True", data[3]));
                            sw.Close();
                        }
                        else
                            MessageBox.Show(kt, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void hàngHoáToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmHangHoa hh = new frmHangHoa();
            hh.ShowDialog();
        }

        private void đơnVịTínhToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmDonViTinh dvt = new frmDonViTinh();
            dvt.ShowDialog();
        }

        private void chủngLoạiHàngToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmLoaiHang lh = new frmLoaiHang();
            lh.ShowDialog();
        }

        SqlCommand cmd;

        public string Restore(string path)
        {
            try
            {
                DataHelper.Open();
                SqlConnection conn = DataHelper.con;
                string UseMaster = "USE master";
                SqlCommand UseMasterCommand = new SqlCommand(UseMaster, conn);
                UseMasterCommand.ExecuteNonQuery();
                string setOff = "use master alter database QLCuaHangBanLe set offline with rollback immediate;";
                SqlCommand setOffCMD = new SqlCommand(setOff, conn);
                setOffCMD.ExecuteNonQuery();

                string Restore = @"RESTORE DATABASE [QLCuaHangBanLe] FROM DISK = N'" + path + @"' WITH REPLACE,  NOUNLOAD,  STATS = 10";
                SqlCommand RestoreCmd = new SqlCommand(Restore, conn);
                RestoreCmd.ExecuteNonQuery();

                string settOnline = "use master alter database QLCuaHangBanLe set online with rollback immediate;";
                SqlCommand setOnlineCMD = new SqlCommand(settOnline, conn);
                setOnlineCMD.ExecuteNonQuery();

                return null;
            }
            catch (SqlException ex)
            {
                return ex.Message + "\t" + ex.GetType().ToString();
            }


        }

        public string Backup(string path)
        {
            try
            {
                DataHelper.Open();
                SqlConnection conn = DataHelper.con;
                string sqlBackup = "BACKUP DATABASE QLCuaHangBanLe TO DISK='" + path + "'";
                cmd = new SqlCommand(sqlBackup, conn);
                cmd.ExecuteNonQuery();
                return null;
            }
            catch (SqlException ex)
            {
                return ex.Message + "\t" + ex.GetType().ToString();
            }
        }

        private void bảoMậtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDoiMatKhau f = new frmDoiMatKhau();
            f.ShowDialog();
            if (f.CloseMain)
            {
                this.Close();
            }
        }

        private void thôngTinTàiKhoảnToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            DataTable dt = DalUsers.XemThongTin(DalUsers.user.TaiKhoan);
            DataRow r = dt.Rows[0];
            frmThongTinTaiKhoan f = new frmThongTinTaiKhoan();
            f.txtTen.Text = r["Ten"].ToString();
            f.txtGioiTinh.Text = r["GioiTinh"].ToString();
            DateTime ngaysinh = (DateTime)r["NgaySinh"];
            f.txtNgaySinh.Text = ngaysinh.ToString("dd/MM/yyyy");
            f.txtSoDienThoai.Text = r["SoDienThoai"].ToString();
            f.txtCMND.Text = r["CMND"].ToString();
            f.txtDiaChi.Text = r["DiaChi"].ToString();
            f.txtTaiKhoan.Text = r["TaiKhoan"].ToString();
            f.txtEmail.Text = r["Email"].ToString();
            f.txtQuyen.Text = r["TenQuyen"].ToString();
            f.ShowDialog();
        }

        private void đăngXuấtToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmDangNhap f = new frmDangNhap();
            f.Show();
            this.Hide();
        }

        private void danhMụcToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmNhanVien f = new frmNhanVien();
            f.ShowDialog();
        }

        private void nhậtKýToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmNhatKyHeThong f = new frmNhatKyHeThong();
            f.ShowDialog();
        }

        private void saoLưuDữLiệuToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = "Đặt tên file, và chọn thư mục để lưu";
            saveFileDialog1.Filter = "Database files (*.mdf)|*.mdf|All files (*.*)|*.*";
            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                string kt = this.Backup(saveFileDialog1.FileName);
                if (kt == null)
                    MessageBox.Show("Sao lưu dữ liệu thành công !",
                        "Thông báo",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                else
                    MessageBox.Show(kt,
                        "Thông báo",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
            }
            
        }

        private void phụcHồiDữLiệuToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "Database files (*.mdf)|*.mdf|All files (*.*)|*.*";
            openFileDialog1.Title = "Chọn file .mdf để khôi phục dữ liệu";
            if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                string kt = this.Restore(openFileDialog1.FileName);
                if (kt == null)
                {
                    MessageBox.Show("Đã khôi phục dữ liệu thành công !",
                        "Thông báo",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show(kt,
                        "Thông báo",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
            }
        }

        private void nhàCungCấpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmNCC f = new frmNCC();
            f.ShowDialog();
        }

        private void chứcNăngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmBanHang f = new frmBanHang();
            f.ShowDialog();
        }

        private void nhậpHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmNhapHang f = new frmNhapHang();
            f.ShowDialog();

        }

        private void tồnKhoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmTonKho f = new frmTonKho();
            f.ShowDialog();
        }

        private void hoáĐơnBánHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmHDBan f = new frmHDBan();
            f.ShowDialog();
        }

        private void hoáĐơnNhậpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmHDNhap f = new frmHDNhap();
            f.ShowDialog();
        }

        private void hoáĐơnXuấtToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmHDXuat f = new frmHDXuat();
            f.ShowDialog();
        }

        private void càiĐặtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmSaoLuuDuLieu f = new frmSaoLuuDuLieu();
            f.ShowDialog();
        }

        private void trợGiúpToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, "demo.chm");
        }

        private void thôngTinPhầnMềmToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmInfo f = new frmInfo();
            f.ShowDialog();
        }

        private void khoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmKhoHang f = new frmKhoHang();
            f.ShowDialog();
        }

        private void saoLưuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.FileName = "Đặt tên file, và chọn thư mục để lưu";
            saveFileDialog1.Filter = "Database files (*.mdf)|*.mdf|All files (*.*)|*.*";
            if (saveFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                string kt = this.Backup(saveFileDialog1.FileName);
                if (kt == null)
                    MessageBox.Show("Sao lưu dữ liệu thành công !",
                        "Thông báo",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                else
                    MessageBox.Show(kt,
                        "Thông báo",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
            }
        }

        private void phụcHồiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = "Database files (*.mdf)|*.mdf|All files (*.*)|*.*";
            openFileDialog1.Title = "Chọn file .mdf để khôi phục dữ liệu";
            if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                string kt = this.Restore(openFileDialog1.FileName);
                if (kt == null)
                {
                    MessageBox.Show("Đã khôi phục dữ liệu thành công !",
                        "Thông báo",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }
                else
                    MessageBox.Show(kt,
                        "Thông báo",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
            }
        }

        private void đổiMậtKhẩuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDoiMatKhau f = new frmDoiMatKhau();
            f.ShowDialog();
            if (f.CloseMain)
            {
                this.Close();
            }
        }

        private void nhậtKýHệThốngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmNhatKyHeThong f = new frmNhatKyHeThong();
            f.ShowDialog();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {

        }

        private void bánHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmBanHang f = new frmBanHang();
            f.ShowDialog();
        }

        private void nhậpHàngToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmNhapHang f = new frmNhapHang();
            f.ShowDialog();
        }

        private void khoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmKhoHang f = new frmKhoHang();
            f.ShowDialog();
        }

        private void đơnVịTínhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDonViTinh dvt = new frmDonViTinh();
            dvt.ShowDialog();
        }

        private void loạiHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLoaiHang lh = new frmLoaiHang();
            lh.ShowDialog();
        }

        private void hàngHoáToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmHangHoa hh = new frmHangHoa();
            hh.ShowDialog();
        }

        private void tồnKhoToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmTonKho f = new frmTonKho();
            f.ShowDialog();
        }

        private void nhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmNhanVien f = new frmNhanVien();
            f.ShowDialog();
        }

        private void đăngXuấtToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmDangNhap f = new frmDangNhap();
            f.Show();
            this.Hide();
        }

        private void ngườiDùngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DataTable dt = DalUsers.XemThongTin(DalUsers.user.TaiKhoan);
            DataRow r = dt.Rows[0];
            frmThongTinTaiKhoan f = new frmThongTinTaiKhoan();
            f.txtTen.Text = r["Ten"].ToString();
            f.txtGioiTinh.Text = r["GioiTinh"].ToString();
            DateTime ngaysinh = (DateTime)r["NgaySinh"];
            f.txtNgaySinh.Text = ngaysinh.ToString("dd/MM/yyyy");
            f.txtSoDienThoai.Text = r["SoDienThoai"].ToString();
            f.txtCMND.Text = r["CMND"].ToString();
            f.txtDiaChi.Text = r["DiaChi"].ToString();
            f.txtTaiKhoan.Text = r["TaiKhoan"].ToString();
            f.txtEmail.Text = r["Email"].ToString();
            f.txtQuyen.Text = r["TenQuyen"].ToString();
            f.ShowDialog();
        }

        string KieuTK = "";

        private void cboKieuTK_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboKieuTK.SelectedIndex > 0)
            {
                txtTimKiem.Visible = true;
                btnTim.Visible = true;
            }
            else
            {
                txtTimKiem.Visible = false;
                btnTim.Visible = false;
            }
            if (cboKieuTK.SelectedIndex == 1)
            {
                KieuTK = "NhanVien";
            }
            if (cboKieuTK.SelectedIndex == 2)
            {
                KieuTK = "NhaCungCap";
            }
            if (cboKieuTK.SelectedIndex == 3)
            {
                KieuTK = "HangHoa";
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (KieuTK == "NhanVien")
            {
                DataTable dt = DalTimKiem.TimKiem(KieuTK, txtTimKiem.Text.Trim());
                frmNhanVien f = new frmNhanVien(dt);
                f.ShowDialog();

            }
            else if (KieuTK == "NhaCungCap")
            {
                DataTable dt = DalTimKiem.TimKiem(KieuTK, txtTimKiem.Text.Trim());
                frmNCC f = new frmNCC(dt);
                f.ShowDialog();

            }
            else if (KieuTK == "HangHoa")
            {
                DataTable dt = DalTimKiem.TimKiem(KieuTK, txtTimKiem.Text.Trim());
                frmHangHoa f = new frmHangHoa(dt);
                f.ShowDialog();

            }
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }

        private void đốiTácToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmNCC f = new frmNCC();
            f.ShowDialog();
        }

        private void hàngĐãBánToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmBaoCao f = new frmBaoCao();
            f.Show();
        }

        private void hàngĐãNhậpToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmBaoCao f = new frmBaoCao();
            f.Show();
        }


    }
}
